package com.ruicar.afs.cloud.image.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.image.entity.ImgDrawStringEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: fangchenliang
 * @Date: 2020/6/20 9:10
 */
@AllArgsConstructor
@Data
@Slf4j
@Component
public class ImageUtil {

    private final DrawImg drawImg;

    public MultipartFile getMulFile(File file, String fileName) {
        FileItem fileItem = createFileItem(file, fileName);
        MultipartFile mfile = new CommonsMultipartFile(fileItem);
        return mfile;
    }

    public FileItem createFileItem(File file, String fileName) {
        String filePath = file.getPath();
        FileItemFactory factory = new DiskFileItemFactory(16, null);
        String textFieldName = "file";
        int num = filePath.lastIndexOf(".");
        String extFile = filePath.substring(num);
        FileItem item = factory.createItem(textFieldName, "multipart/form-data", true, fileName);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        FileInputStream fis = null;
        OutputStream os = null;
        try {
            fis = new FileInputStream(file);
            os = item.getOutputStream();
            while ((bytesRead = fis.read(buffer, 0, 8192))
                    != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            log.error("文件处理失败:" + e.getMessage());
        } finally {
            IoUtil.close(fis);
            IoUtil.close(os);
        }
        return item;
    }

    public byte[] setWatermark(byte[] bytes,String username,String fileType){
        InputStream in=new ByteArrayInputStream(bytes);
        BufferedImage inputBufferedImage=null;
        try {
            inputBufferedImage=ImageIO.read(in);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Font font = new Font("宋体",Font.BOLD,35);
        List<ImgDrawStringEntity> list =new ArrayList<>();
        ImgDrawStringEntity timeEntity=new ImgDrawStringEntity();
        timeEntity.setAlpha(Float.valueOf("0.4"));
        timeEntity.setContent(DateUtil.format(new Date(),"yyyy-MM-dd hh:mm:ss"));
        timeEntity.setX(-20);
        timeEntity.setY(80);
        timeEntity.setFont(font);
        timeEntity.setContentColor(Color.red);
        timeEntity.setRotate(Double.valueOf("345"));
        list.add(timeEntity);

        ImgDrawStringEntity userEntity=new ImgDrawStringEntity();
        userEntity.setAlpha(Float.valueOf("0.4"));
        userEntity.setContent(username);
        userEntity.setX(-20);
        userEntity.setY(50);
        userEntity.setFont(font);
        userEntity.setContentColor(Color.red);
        userEntity.setRotate(Double.valueOf("0"));
        list.add(userEntity);

        BufferedImage bufferedImage=drawImg.drawStringOnImg(inputBufferedImage,list);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, fileType, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

}
